-
-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: adds missing reswap modifier and adds reswap builder along with StopPolling #23
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Im not sure about this. It's not necessarily a bad idea to to have a builder that helps users create the right combination of swap modifiers. However, I want to hold off on this for a little until I better understand htmx myself.
/// <returns>This <see cref="HtmxResponse"/> object instance.</returns> | ||
public HtmxResponse Reswap(SwapStyle swapStyle) | ||
public HtmxResponse Reswap(SwapStyle swapStyle, string? modifier = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this be compatible with hx-swap="show:none"
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Took a dive into the htmx source. It seems like modifiers can be used without necessarily specifying a swap style. The swap specification is loaded over top of the defaults. It defaults swapStyle to innerHTML if boosted, otherwise whatever the configured defaultSwapStyle is. Then reswap can come in and change the swapstyle and/or add modifiers. Modifiers can be used independently of Swapstyle.
I'm going to overload Reswap with just a string parameter to allow modifiers only.
I added the Reswap overload. Some comments on the builder. It's patterned after a builder that is widely used with the go programming language: The difference is that they have an additional swapstyle option called "Default" that is used in cases where there is no swap style used in the reswap. Since SwapStyle is used in configurations and AjaxRequest you'd need to write a custom serializer to include "Default" as a SwapStyle option, even if semantically probably a correct option. I updated the SwapStyleBuilder and added a suite of tests to validate the builder. I also rewrote it to override existing modifiers if accidentilly calling the same modifier. It's worth reconsidering because on the server side for reswaps you have to construct all the modifiers in the correct format by hand which is more error prone than using the builder. You can always consider it experimental for the time being and toss if you end up not liking it as it doesn't interfere with using the other Reswap methods as it has a pretty self-contained footprint. |
75f4ef6
to
9cba39e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think you have a good point about including this in the package. Lets iterate on the API a bit though!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice job adding tests. Much appreciated.
For this project I want to standardize on FluentAssertions, so please use it for assertions.
Can you elaborate more on what this means (remember I am an htmx newbie). Could we add a |
Ps. do also rebase your branch on main. Did a major clean up this evening. |
… StopPolling This commit introduces several enhancements to the `HtmxResponse` class. It adds a new optional parameter `modifier` to the `Reswap` method, allowing for more flexible response swapping. A new overload of the `Reswap` method is also added that takes a `SwapStyleBuilder` object as an argument. A new method, `StopPolling`, has been introduced which sets the response code to stop polling. Two new classes, `HtmxStatusCodes` and `SwapStyleBuilder`, have been created. The former provides status codes specific to HTMX responses while the latter aids in constructing swap style command strings for HTMX responses. Additionally, a new enum called `ScrollDirection` has been added for specifying scroll direction values. refactor: Add Reswap method and refactor SwapStyleBuilder Added a new Reswap method to the HtmxResponse class, allowing for more flexible response swapping. Refactored the SwapStyleBuilder class to use an OrderedDictionary for storing modifiers instead of a List, improving performance and readability. Also added nullability support for the SwapStyle property in SwapStyleBuilder. Added comprehensive unit tests for these changes in SwapStyleBuilderTests.cs. feat: Add ShowNone method to SwapStyleBuilder A new method, ShowNone, has been added to the SwapStyleBuilder class. This method turns off scrolling after a swap and allows for chaining with other methods in the class. Corresponding unit test has also been added to ensure that it returns the correct value.
Having a "Default" option in SwapStyle could have meaning in several contexts but in many of them it is simply a no-op of sorts:
In some ways it's like setting a value to true/false/unset. What it does also do is allows you to also build fluent chains off of SwapStyle.Default as well, so SwapStyle.Default.ShowNone() builds a reswap as "show:none" only. Likewise, SwapStyle.Default.After(TimeSpan.FromSeconds(1)).ScrollTop().ShowTransition().IgnoreTitle() builds modifiers only with no SwapStyle override. |
Significant enhancements have been made to the SwapStyleBuilder class. The class has been sealed and several new methods have been added to provide more granular control over swap style commands. These include methods for controlling scroll direction, transition effects, title inclusion, and delay times for both swap and settle operations. Existing methods have also been updated with more detailed comments and improved parameter naming for better clarity.
1e5df1e
to
76509fb
Compare
Enhanced the SwapStyleBuilder class by adding new methods to control viewport positioning after a swap operation. This includes methods for specifying whether the viewport should be positioned at the top or bottom of the swap target, and whether it should scroll to a focused element when a request completes. Also added corresponding tests to verify these new features.
New unit tests have been added to the `SwapStyleBuilderTests` class. These tests cover the `ShowOnTop`, `ShowOnBottom`, and `MixedShowOverrides` methods, ensuring they return correct values. The inheritance from `TestContext` has also been removed from this class.
Here is the full set of builder methods:
|
So remaining issues:
|
Updated the SwapStyleBuilder class and its extension to improve code readability and maintainability. Changes include: - Modified method descriptions to provide more accurate information about their functionality. - Updated parameter names and descriptions for better understanding of their purpose. - Enhanced remarks sections with additional details about the methods' behavior.
Would it work if
I will go over them and report back. |
Enhanced the SwapStyleBuilder's Scroll and ShowOn methods to allow for more precise control over scrollbar positioning after a content swap. The methods now accept an optional CSS selector parameter, which can be used to specify a target element for scrolling. If provided, the page will scroll to the top or bottom of the selected element after swapping content. Removed redundant code related to smooth animation of scrollbar position. Updated corresponding unit tests accordingly.
I refactored the code a bit and just added a nullable selector for Scroll and ShowOn when selectors are used. I updated the list of methods. |
The ordering of what swap style is used looks like this (in order of precedence):
For reswaps it is:
The impact of adding SwapStyle.Default as an option would mean that it is possible that a developer could set HtmxConfig.DefaultSwapStyle to SwapStyle.Default. When emitting the configuration code, however, the value for DefaultSwapStyle cannot be 'default' as that is an invalid value. So one approach might be to override the setter on the configuration default swap style and ignore attempts to set it to SwapStyle.Default. |
That makes sense. Lets do that. Tried to pull the PR down and make some changes but it looks like I do not have permissions to push to your branch. |
Getting a Htmxor-c7a345e-fix tweaks to code docs, added default swap style.patch |
As far as the swap style builder is concerned, the build method returns the style and modifiers as a tuple. Would it be better for the builder to create strings so it could alternatively be used to create swaps for hx-get as well? Your example was: <div hx-swap=@SwapStyle.InnerHTML.ScrollTop() hx-get="..."></div> I think for this to work I would have to end the chain with Build() or ToString(). <div hx-swap=@SwapStyle.InnerHTML.ScrollTop().Build() hx-get="..."></div>
<div hx-swap=@SwapStyle.InnerHTML.ScrollTop().ToString() hx-get="..."></div> |
btw. excellent work with the code docs. Really nice that users wont have to go to the htmx docs, they have all they need in the editor! |
The builder should implement a |
I added write access. |
Think you need to do this to allow me to push: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork |
Looking into this. I don't have an "Allow edits and access to secrets by maintainers" option. |
ok, ill just merge this, create a PR on my own, and you can provide your suggestions :) |
This commit introduces several enhancements to the
HtmxResponse
class. It adds a new optional parametermodifier
to theReswap
method, allowing for more flexible response swapping. A new overload of theReswap
method is also added that takes aSwapStyleBuilder
object as an argument to allow for fluent modifiers.See Reswap fluent modifiers docs: https://jalexsocial.github.io/rizzy.docs/docs/htmx/response/#applying-fluent-modifiers
A new method,
StopPolling
, has been introduced which sets the response code to stop polling.Two new classes,
HtmxStatusCodes
andSwapStyleBuilder
, have been created. The former provides status codes specific to HTMX responses while the latter aids in constructing reswap commands.Additionally, a new enum called
ScrollDirection
has been added for specifying scroll direction values for reswaps.